
#只考虑发年终奖当月，工资满5000的情况
import getopt
import sys


class BonusTaxCalculator:
               # 税级        税率       速算扣除数    临界区下限    临界区上限
    tax_table = {0:         [0.03,      0,          0,          0],
                 1:         [0.1,       210,        36000,      38566.67],
                 2:         [0.2,       1410,       144000,     160500.00],
                 3:         [0.25,      2660,       300000,     318333.33],
                 4:         [0.30,      4410,       420000,     447500.00],
                 5:         [0.35,      5505,       660000,     706538.46],
                 6:         [0.45,      15160,      960000,     1120000.00]}
    def __init__(self, bonus):
        self.bonus = bonus
        self.level = -1

    def getLevel(self, income):
        if income < 3000:
            return 0
        elif income < 12000:
            return 1
        elif income < 25000:
            return 2
        elif income < 35000:
            return 3
        elif income < 55000:
            return 4
        elif income < 80000:
            return 5
        else:
            return 6

    def calculate(self):
        monthIncome = self.bonus / 12
        self.level = self.getLevel(monthIncome)
        config = self.tax_table[self.level]
        self.finalIncome = self.bonus * (1- config[0]) + config[1]
        self.tax = self.bonus - self.finalIncome
        return self.finalIncome

    def printDetails(self):
        if (self.level < 0):
            raise Exception("Invoke calculate first")
        config = self.tax_table[self.level]
        print("\033[4;37;40mIncome before tax :%f\n\033[0m"
              "\033[4;37;40mIncome after tax  :%f\n\033[0m"
              "\033[4;37;40mTax               :%f\n\033[0m"
              "Tax level         :%d\n"
              "\033[4;37;40mTax rate          :%f\n\033[0m"
              "Quick deduction   :%d\n"
              "\033[4;37;40mCritical floor    :%d\n\033[0m"
              "\033[4;37;40mCritical ceiling  :%d\033[0m"
              % (self.bonus, self.finalIncome, self.tax, self.level, config[0], config[1], config[2], config[3]))

    @classmethod
    def printAllTaxLevels(cls):
        for i in range(7):
            config = cls.tax_table[i]
            print("Level             %d >>>\n"
                  "Tax rage          %f\n"
                  "Quick deduction   %d\n"
                  "\033[4;37;40mCritical floor    %d\n\033[0m"
                  "\033[4;37;40mCritical ceiling  %d\033[0m"
                  % (i, config[0], config[1], config[2], config[3]))


def printUsage():
    print("Usage:\n"
          "-h: print this message\n"
          "-l: print all tax details on each level\n"
          "-b: input bonus before tax")
    sys.exit(1)


if __name__ == '__main__':
    opts, args = getopt.getopt(sys.argv[1:], "hb:l")
    if len(sys.argv) < 2:
        printUsage()
        sys.exit(1)
    for op, value in opts:
        if op == "-h":
            printUsage()
        elif op == "-b":
            bonus = float(value)
            calculator = BonusTaxCalculator(bonus)
            calculator.calculate()
            calculator.printDetails()
        elif op == "-l":
            BonusTaxCalculator.printAllTaxLevels()
        else:
            print("Unsupported option ", op)
            sys.exit(1)



